//
//  BKNewViiewModel.m
//  baokaodaxue
//
//  Created by dangyangyang on 15/6/17.
//  Copyright (c) 2015年 宏图教育. All rights reserved.
//

#import "BKNewViiewModel.h"
#import <AFNetworking.h>

@implementation BKNewViiewModel
+ (void)newsWithType:(NSString *)type success:(void (^)(NSArray *))success failure:(void (^)(NSString *))failure{

    
    NSDictionary *params = @{@"type":type,
                             @"page":@"1",
                             @"len":@"10000"};
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [manager.requestSerializer setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];

    [manager GET:@"http://www.baokaodaxue.com/bk/mobile/news" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *responseDic=[NSJSONSerialization
                                   JSONObjectWithData:responseObject
                                   options:NSJSONReadingMutableLeaves
                                   error:nil];
        if ([[responseDic objectForKey:@"code"] isEqualToString:@"200"]) {
            
            NSDictionary *extraData = responseDic[@"extraData"];
            NSArray *dataArray = extraData[@"info"];
            if ([dataArray count]==0) {
                failure(@"已加载完成");
            }else{
                
                
                success(dataArray);
            }
            
        }else{
            failure([responseDic objectForKey:@"message"]);
            
        }

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
         failure(@"网络连接失败");
    }];
    

}

+ (void)detailWithId:(NSString *)articleId success:(void (^)(NSDictionary *))success failure:(void (^)(NSString *))failure{

    NSDictionary *params = @{@"id":articleId};
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [manager.requestSerializer setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    
    [manager GET:@"http://www.baokaodaxue.com/bk/mobile/newsdetail" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *responseDic=[NSJSONSerialization
                                   JSONObjectWithData:responseObject
                                   options:NSJSONReadingMutableLeaves
                                   error:nil];
        if ([[responseDic objectForKey:@"code"] isEqualToString:@"200"]) {
            
            NSDictionary *extraData = responseDic[@"extraData"];
          
            success(extraData);
            
        }else{
            failure([responseDic objectForKey:@"message"]);
            
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failure(@"网络连接失败");
    }];
    

}
@end
